function N= func_wrapper_monthbymonth(principal_data,teacher_data ,store_suffix, rc_on, N      )



%% load principal prefs and clean them up


% this works: for a full string
folder='../../01_data/05_temp/';
fullmatfilename=fullfile(folder,principal_data);
load(fullmatfilename)

%load('../../01_data/05_temp/mixed_logit_principal_totVA_extraX')
 
   
    IJX_princ=[ ones(length(IJX_princ),1) IJX_princ];   % add a vector of ones  
    IJ_princ=IJX_princ * thetahat(1:end-1,1);     % don't include RC
    clearvars -except IJ_princ IJN* IJVA* principal_data teacher_data store_suffix N rc_on year posting_month

    
folder='../../01_data/05_temp/';
fullmatfilename=fullfile(folder, teacher_data);
load(fullmatfilename)
%load('../../01_data/05_temp/mixed_logit_rc6_totVA_FRPL')

 
if rc_on==1
    % select the best Xdraws and clean them up
    [Xdraws_best] = func_likely_draws_current(IJitidx, IJchoiceset,IJcurrentFirst, Xdraws,Xmat,thetahat,y_logistic);
    
    % redo Xdraws_best to be as long as everything else
    [Ci, IAi, ICi]=unique(IJitidx(IJchoiceset==1));
    Xdraws_unique=Xdraws_best(IAi,:);
    Xdraws_best2=Xdraws_unique(IJitidx,:);
    
    % find predicted values in each match (teacher preferences)
    
    IJipref=[Xmat Xdraws_best2]*thetahat;
end

if rc_on==0
    
    IJipref=xb;
end
    
    % generate IJpref that is -999999999 if choiceset=0
    
    IJipref_choice=IJchoiceset.*IJipref + (1-IJchoiceset)*(-9999999999);
    
    IJipref_nontitleI_choice=IJipref.*titleI + (1-titleI).*IJchoiceset.*IJipref + (1-titleI).*(1-IJchoiceset)*(-999999999999);  

    
    % generate IJfracDisadv that is -99999999 if choicset=0
    
    IJfracDisadv_choice=IJchoiceset.*IJfracDisadv + (1-IJchoiceset)*(-9999999999);
 
    IJNdis= IJclassSize.*IJfracDisadv;
    IJNdis_choice=IJchoiceset.*IJNdis + (1-IJchoiceset)*(-9999999999);

    
    % build school preferences (by changing this block, can make it principal preferences, or....)
      
    IJoutput=IJclassSize.*IJVAma;
    IJoutput_dis=IJclassSize.*IJfracDisadv.*IJVAm2;
    IJoutput_adv=IJclassSize.*(1-IJfracDisadv).*IJVAm1;
    
  % generate ouput that is -999999999 if choiceset=0
    
    IJoutput_choice=IJchoiceset.*IJoutput + (1-IJchoiceset)*(-9999999999);   
    IJoutput_dis_choice=IJchoiceset.*IJoutput_dis + (1-IJchoiceset)*(-9999999999);   
    IJoutput_adv_choice=IJchoiceset.*IJoutput_adv + (1-IJchoiceset)*(-9999999999);   

    % generate IJ_princ that is -99999 if choiceset=0

    IJ_princ_choice=IJchoiceset.*IJ_princ + (1-IJchoiceset)*(-9999999999);  
    IJ_choice=(1-IJchoiceset)*(-9999999999);
    % generate principal for non-title I, and VA for title I
    
    % generate a principal variable that has complete choice sets if title
    % I, but restricts choice sets if non-title
    
    IJ_princ_nontitleI_choice=IJ_princ.*titleI + (1-titleI).*IJchoiceset.*IJ_princ + (1-titleI).*(1-IJchoiceset)*(-999999999999);  

    
    % generate 4 variables from the x matrix
    
    IJblack=Xmat(:,30);
    IJexp23=Xmat(:,33);
    IJexp46=Xmat(:,34);
    IJexp7p=Xmat(:,35);
    
    
    % in 2015, generate distribution of vacancy posting months and month
    % first app, across the three subperiods
    
    month_first_app2015=month_first_app(IJappyear==2015);
    vacancy_posting_month2015=vacancy_posting_month(IJappyear==2015);
    titleI2015=titleI(IJappyear==2015);
    
    IJitidx2015=IJitidx(IJappyear==2015);
    IJjtidx2015=IJjtidx(IJappyear==2015);
    IJitidx2015=IJitidx2015-min(IJitidx2015)+1;
    IJjtidx2015=IJjtidx2015-min(IJjtidx2015)+1;
    
   for i=1:length(IJitidx2015)
        vacancy_posting_month_sq(IJitidx2015(i,1),IJjtidx2015(i,1))=vacancy_posting_month2015(i,1);  % these are I X J (useful for first best)    
        month_first_app_sq(IJitidx2015(i,1),IJjtidx2015(i,1))=month_first_app2015(i,1);  % these are I X J (useful for first best)       
        titleI_sq(IJitidx2015(i,1),IJjtidx2015(i,1))=titleI2015(i,1);  % these are I X J (useful for first best)       

   end    
   
   titleI_post=titleI_sq(1,:)';
   
   post_april=(vacancy_posting_month_sq(1,:)<=4)';
   app_april=(month_first_app_sq(:,1)<=4);
   
   post_june=(vacancy_posting_month_sq(1,:)<=6 & vacancy_posting_month_sq(1,:)>4 )';
   app_june=(month_first_app_sq(:,1)<=6 & month_first_app_sq(:,1)>4 );
   
   post_aug=(vacancy_posting_month_sq(1,:)>6)';
   app_aug=(month_first_app_sq(:,1)>6);
   
   post_apr_sh=sum(post_april)/length(post_april);
   post_june_sh=sum(post_june)/length(post_june);
   post_aug_sh=sum(post_aug)/length(post_aug);
   
   app_april_sh=sum(app_april)/length(app_april);
   app_june_sh=sum(app_june)/length(app_june);
   app_aug_sh=sum(app_aug)/length(app_aug);
   
   % arrange posting by title I 
   post_april_titleI=post_april.*titleI_post;
   post_april_nontitleI=post_april.*(1-titleI_post);
   
   post_june_titleI=post_june.*titleI_post;
   post_june_nontitleI=post_june.*(1-titleI_post);
   
   post_aug_titleI=post_aug.*titleI_post;
   post_aug_nontitleI=post_aug.*(1-titleI_post);
   
   post_apr_titleI_sh=sum(post_april_titleI)/sum(titleI_post);
   post_apr_nontitleI_sh=sum(post_april_nontitleI)/sum(1-titleI_post);
   
   post_june_titleI_sh=sum(post_june_titleI)/sum(titleI_post);
   post_june_nontitleI_sh=sum(post_june_nontitleI)/sum(1-titleI_post);
   
   post_aug_titleI_sh=sum(post_aug_titleI)/sum(titleI_post);
   post_aug_nontitleI_sh=sum(post_aug_nontitleI)/sum(1-titleI_post);
   
   % collect for output
   app_sh=[app_april_sh app_june_sh app_aug_sh];
   post_sh=[post_apr_sh post_june_sh post_aug_sh];
   post_titleI_sh=[post_apr_titleI_sh post_june_titleI_sh post_aug_titleI_sh];
   post_nontitleI_sh=[post_apr_nontitleI_sh post_june_nontitleI_sh post_aug_nontitleI_sh];

   
base=['app_posting_timing'];
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, base);
save(savefile,'app_sh', 'post_sh','post_titleI_sh','post_nontitleI_sh');   

%% 1. status quo, principal pref, restricted choice sets



balanced_store=zeros(N,54);


    for kk=1:N
        

       error_draws_princ=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both    
       error_draws_teach=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both  
          
      balanced_store(kk,:)= func_allocations_monthbymonth( IJipref+error_draws_teach,IJipref_choice+error_draws_teach,IJ_princ_choice+error_draws_princ, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJschoolidx,IJappyear,titleI,vacancy_posting_month,month_first_app,2015,1,0);
 
    end 
    

    
  balanced=mean(balanced_store);

    
base=['alloc_teach_est_princ_est_restr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'balanced');
    
clearvars balanced balanced_store  



%% 2. status quo, principal pref, unrestricted for title I, restricted for non-Title I 



balanced_store=zeros(N,54);




    for kk=1:N

       error_draws_princ=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both    
       error_draws_teach=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both  
      
               
      balanced_store(kk,:)= func_allocations_monthbymonth( IJipref+error_draws_teach,IJipref_nontitleI_choice+error_draws_teach,IJ_princ_nontitleI_choice+error_draws_princ, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJschoolidx,IJappyear,titleI,vacancy_posting_month,month_first_app,2015,1,0);
             
    end 
    
  balanced=mean(balanced_store);

    
base=['alloc_teach_est_princ_est_nontitleI_restr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'balanced');
    
clearvars balanced balanced_store 



%% 2. status quo, principal pref, unrestricted 



balanced_store=zeros(N,54);




    for kk=1:N

       error_draws_princ=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both    
        error_draws_teach=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both  
      
               
      balanced_store(kk,:)= func_allocations_monthbymonth( IJipref+error_draws_teach,IJipref+error_draws_teach,IJ_princ+error_draws_princ, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJschoolidx,IJappyear,titleI,vacancy_posting_month,month_first_app,2015,1,0);
             
    end 
    
  balanced=mean(balanced_store);

    
base=['alloc_teach_est_princ_est_unrestr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'balanced');
    
clearvars balanced balanced_store


%% 4. status quo, principal noisy, restricted choice sets



balanced_store=zeros(N,54);




    for kk=1:N

       error_draws_princ=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both    
       error_draws_teach=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both  
       
               
      balanced_store(kk,:)= func_allocations_monthbymonth( IJipref+error_draws_teach,IJipref_choice+error_draws_teach,IJ_choice+error_draws_princ, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJschoolidx,IJappyear,titleI,vacancy_posting_month,month_first_app,2015,1,0);
             
    end 
    
  balanced=mean(balanced_store);

    
base=['alloc_teach_est_princ_noisy_restr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'balanced');
    
clearvars balanced balanced_store 


%% 5. status quo, principal VA, restricted choice sets



balanced_store=zeros(N,54);




    for kk=1:N

       error_draws_princ=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both    
        error_draws_teach=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both  
      
               
      balanced_store(kk,:)= func_allocations_monthbymonth( IJipref+error_draws_teach,IJipref_choice+error_draws_teach,IJoutput_choice, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJschoolidx,IJappyear,titleI,vacancy_posting_month,month_first_app,2015,1,0);
        
      
    end 
    
  balanced=mean(balanced_store);

    
base=['alloc_teach_est_princ_va_restr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'balanced');
    
clearvars balanced  balanced_store 



%% 6. status quo, principal VA, restricted choice sets, teahcer max Ndis



balanced_store=zeros(N,54);
 



    for kk=1:N

       error_draws_princ=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both    
       error_draws_teach=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both  

       
               
      balanced_store(kk,:)= func_allocations_monthbymonth( IJipref,IJNdis_choice,IJ_princ_choice+error_draws_princ, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJschoolidx,IJappyear,titleI,vacancy_posting_month,month_first_app,2015,1,0);
         
       
    end 
    
  balanced=mean(balanced_store);

    
base=['alloc_teach_ndis_princ_est_restr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'balanced');
    
clearvars balanced  balanced_store 


%% 7. status quo, principal va, restricted choice sets, teahcer max Ndis



balanced_store=zeros(N,54);




    for kk=1:N

       error_draws_princ=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both    
       error_draws_teach=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both  
       
               
      balanced_store(kk,:)= func_allocations_monthbymonth( IJipref,IJNdis_choice,IJoutput_choice, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJschoolidx,IJappyear,titleI,vacancy_posting_month,month_first_app,2015,1,0);
        
       
    end 
    
  balanced=mean(balanced_store);

    
base=['alloc_teach_ndis_princ_va_restr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'balanced');
    
clearvars balanced  balanced_store 



%% 8. status quo, principal max VA for disadvantaged, unrestricted choice sets, also compute first best 


balanced_store=zeros(N,54);



    for kk=1:N

       error_draws_princ=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both    
       error_draws_teach=evrnd(0,1,length(IJitidx),1);    % want the same set of errors for both  
  
       
               
      balanced_store(kk,:)= func_allocations_monthbymonth( IJipref+error_draws_teach,IJipref+error_draws_teach,IJoutput_dis, IJclassSize,IJVAm1, IJVAm2,IJfracDisadv, IJitidx,IJjtidx,IJschoolidx,IJappyear,titleI,vacancy_posting_month,month_first_app,2015,1,1);
                  
    end 
    
  balanced=mean(balanced_store);
    
base=['alloc_teach_est_princ_va_unrestr_',store_suffix];
basefilename=join(base);
folder='../../01_data/05_temp/model/';
savefile=fullfile(folder, basefilename);
save(savefile,'balanced');
    
clearvars balanced   balanced_store
  
  
end 



    
 